package com.springlibrary.controllers;

import java.io.IOException;
import java.util.Date;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import javax.portlet.ProcessEvent;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.EventMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.PortletDisplay;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.PortletURLFactoryUtil;
import com.springlibrary.slayer.model.LMSBook;
import com.springlibrary.slayer.model.impl.LMSBookImpl;
import com.springlibrary.slayer.service.LMSBookLocalServiceUtil;

@Controller
@RequestMapping("VIEW")
public class AddBookController {
	@Autowired
	private BookValidator bookValidator;

	public BookValidator getBookValidator() {
		return bookValidator;
	}

	public void setBookValidator(BookValidator bookValidator) {
		this.bookValidator = bookValidator;
	}

	@RenderMapping
	public String handleRenderRequest(RenderRequest renderRequest,
			RenderResponse renserResponse, Model model) {
		ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
        //System.out.println("help mode enabled :: " + portletDisplay.isShowHelpIcon());
        portletDisplay.setShowHelpIcon(true);
        //System.out.println("edit mode enabled :: " + portletDisplay.isShowEditIcon());
        portletDisplay.setModeHelp(true);
		return "view";
	}

	@RenderMapping(params = "action=update")
	public String update(Model model) {
		if (!model.containsAttribute("lmsBook")) {
			model.addAttribute("lmsBook", new LMSBookImpl());
		}
		return "update";
	}

	@RenderMapping(params = "action=updateForm")
	public String updateForm(Model model, RenderRequest renderRequest)
			throws PortalException, SystemException {
		long bookId = ParamUtil.getLong(renderRequest, "bookId");
		LMSBook lmsBook = LMSBookLocalServiceUtil.getLMSBook(bookId);
		model.addAttribute("lmsBook", lmsBook);
		return "update";
	}

	@RenderMapping(params = "action=list")
	public String list() {
		return "list";
	}

	@ActionMapping
	public void addBook(@ModelAttribute("lmsBook") LMSBookImpl command,
			BindingResult result, SessionStatus status,
			ActionResponse response, ActionRequest actionRequest)
			throws Exception {
		bookValidator.validate(command, result);
		System.out.println("result.hasErrors() ::" + result.hasErrors());
		if (result.hasErrors()) {
			System.out.println(command.toString());
			status.setComplete();
			response.setRenderParameter("action", "update");
		} else {
			command.setBookId(CounterLocalServiceUtil.increment(LMSBook.class
					.getName()));
			command.setCreateDate(new Date());
			LMSBookLocalServiceUtil.addLMSBook(command);

			ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
					.getAttribute(WebKeys.THEME_DISPLAY);

			String portletName = ParamUtil.getString(actionRequest,
					"rootPortletId");
			PortletURL successPageURL = PortletURLFactoryUtil.create(
					actionRequest, portletName, themeDisplay.getPlid(),
					PortletRequest.RENDER_PHASE);
			successPageURL.setParameter("action", "list");
			response.sendRedirect(successPageURL.toString());
		}
	}

	@ActionMapping(params = "action=updateBook")
	public void updateBook(@ModelAttribute("lmsBook") LMSBookImpl command,
			BindingResult result, SessionStatus status,
			ActionResponse response, ActionRequest actionRequest)
			throws Exception {
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
				.getAttribute(WebKeys.THEME_DISPLAY);
		command.setCreateDate(new Date());
		LMSBookLocalServiceUtil.updateLMSBook(command);
		response.sendRedirect(PortalUtil.getLayoutURL(themeDisplay));
	}

	@ActionMapping(params = "action=deleteBook")
	public void deleteBook(@ModelAttribute("lmsBook") LMSBookImpl command,
			BindingResult result, SessionStatus status, ActionResponse response)
			throws Exception {
		long bookId = command.getBookId();
		if (bookId > 0L) {
			LMSBookLocalServiceUtil.deleteLMSBook(bookId);
		}
	}

	@EventMapping
	@ProcessEvent(qname = "{http://liferay.com}userinfo")
	public void handleProcessempinfoEvent(EventRequest request,
			EventResponse response) throws PortletException, IOException {
		javax.portlet.Event event = request.getEvent();
		String value = (String) event.getValue();
		response.setRenderParameter("Name", value);

	}
}
